package com.bub.pay.framework.common.entity.model;

import com.bub.pay.framework.common.util.RsaKeyHelper;
import org.apache.commons.codec.binary.Base64;

import java.security.Key;

public class RsaKey {

    /**
     * 公钥
     */
    private Key publicKey;

    /**
     * 私钥
     */
    private Key privateKey;

    public RsaKey() {
    }

    public RsaKey(Key publicKey, Key privateKey) {
        this.publicKey = publicKey;
        this.privateKey = privateKey;
    }

    public RsaKey(String publicKey, String privateKey) throws Exception {
        this.publicKey = RsaKeyHelper.parsePublicKey(publicKey);
        this.privateKey = RsaKeyHelper.parsePrivateKey(privateKey);
    }

    public Key getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(Key publicKey) {
        this.publicKey = publicKey;
    }

    public Key getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(Key privateKey) {
        this.privateKey = privateKey;
    }

    /**
     * 获取公钥base64格式
     *
     * @return 公钥base64格式
     */
    public String getPublicKeyBase64() {
        byte[] publicKeyBytes = publicKey.getEncoded();
        byte[] base64Bytes = Base64.encodeBase64(publicKeyBytes, false);
        return new String(base64Bytes);
    }

    /**
     * 获取私钥base64格式
     *
     * @return 私钥base64格式
     */
    public String getPrivateKeyBase64() {
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] base64Bytes = Base64.encodeBase64(privateKeyBytes, false);
        return new String(base64Bytes);
    }

}
